package com.mti.epivisite.common
{
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class UserProxy extends Proxy
	{
		public static const NAME:String = "UserProxy";
		
		/**
		 * Create a new user proxy corresponding to a login.
		 * Throw a DatabaseError if the user does not exist in the database.
		 */
		public static function createUserProxy(login:String):UserProxy
		{
			var proxy:UserProxy = tryCreateUserProxy(login);
			if (proxy == null)
				throw new DatabaseError("Unable to get user with login " + login + " from database.", 0);
			return proxy;
		}
		
		/**
		 * Create a new user proxy corresponding to a login.
		 * Return null if the user does not exist in the database.
		 */
		public static function tryCreateUserProxy(login:String):UserProxy
		{
			var user:UserVO = getUserWithLogin(login);
			if (user == null)
				return null;
			var proxy:UserProxy = new UserProxy(NAME + login, user);
			return proxy;
		}
		
		/**
		 * Retrieve a user from the database
		 * and create a VO for it.
		 */
		private static function getUserWithLogin(login:String):UserVO
		{
			var user:UserVO = new UserVO();
			user.login = "UserTest";
			return user;
		}
		
		/**
		 * Create a new UserProxy instance.
		 * Use the static methods createUserProxy or tryCreateUserProxy
		 * to create a proxy from a user login.
		 */
		public function UserProxy(proxyName:String, data:UserVO)
		{
			super(proxyName, data);
		}
		
		public function get user():UserVO
		{
			return UserVO(data);
		}
	}
}